(function() {
  app.controller('vipHoldCtrl', function($scope, local_m, vipIndex, $state, bottomdilog, tradeApi) {
    $scope.dir_cards = {
      data: [],
      config: {
        title: {
          css: {},
          content: { 'key1': 'stkcode', 'key2': 'stkname', 'split': '--' },
        },
        rows: {
          css: {
            left: {},
            right: {
              color: '#333333'
            }
          },
          content: [
            { txt: '参考盈亏', key: 'income', unit: '元', css: { color: 'card_item_txt_color', } },
            { txt: '参考盈亏率', key: 'incomeratio', css: { color: 'card_item_txt_color', } },
            { txt: '参考市值', key: 'marketval', unit: '元' },
            { txt: '持仓数量', key: 'amt', unit: '股' },
            { txt: '可用数量', key: 'enable', unit: '股' },
            { txt: '参考成本', key: 'price', unit: '元 / 股' },
            { txt: '最新价格', key: 'lastprice', unit: '元 / 股' },
          ],
        },
        buttons: {
          ok_show: true,
          cancel_show: true,
          ok_txt: '买入',
          cancel_txt: '卖出',
          ok: function(obj) {
            $scope.to_trade(obj, 'buy');
          },
          cancel: function(obj) {
            $scope.to_trade(obj, 'sell');
          }
        },
        state: {
          show: false,
          key: ''
        }
      },
    };


    $scope.init = function() {
      tradeApi
        .etQuery({
          funcid: '4',
        })
        .then(function(res) {
          angular.forEach(res, function(item) {
            if (item.income > 0) {
              item.card_item_txt_color = 'red';
            } else {
              item.card_item_txt_color = '#30ae0e';
            }
          });
          $scope.dir_cards.data = res;
        });
    };

    $scope.to_trade = function(item, type) {
      $state.go('vip-trade', {
        type: type,
        stkcode: item.obj.stkcode,
      });
    };
  });
})();
